<?php
/**
 * @Description 模型验证模块
 * @Sonkwl Xiong
 * @Date 2022/02/22 11:14:14
 */
namespace procjson;

class model{
    static $json=array();
    //常规请求模型
    static public $model_command=array(
        "jwt"=>array("command","token"),
        "login"=>array("command","table","clause","id"),
        "query"=>array("command","table","clause"),
        "select"=>array("command","table","clause"),
        "insert"=>array("command","table","clause"),
        "update"=>array("command","table","clause","update"),
        "delete"=>array("command","table","clause"),
    );
    //@Sonkwl Xiong 2022/02/22 10:16:47
    //@Desc 验证请求模型
    static public function CheckCommand($step){
        if(!isset($step["command"])){
            self::$json["res"]="NG";
            self::$json["message"]="没有定义command";
            echo json_encode(self::$json);
            exit;
        }
        if(!isset(self::$model_command[$step["command"]])){
            self::$json["res"]="NG";
            self::$json["message"]=$step["command"]."没有对应的模型";
            echo json_encode(self::$json);
            exit;
        }
        $model_obj=self::$model_command[$step["command"]];
        foreach($model_obj as $v){
            if(!in_array($v,array_keys($step))){
                self::$json["res"]="NG";
                self::$json["message"]=$step["command"]."中需要定义".$v;
                echo json_encode(self::$json);
                exit;
            }
        }
    }

    //@Desc 开放模型
    static public $model_open=array(
        //user table 测试用例
        // "user"=>array(
        //     //登录模型
        //     "login"=>array(
        //         "command"=>"login",
        //         "table"=>"user",
        //         "clause"=>array("s_no","s_pwd"),
        //         "id"=>"s_no"//返回jwt token的栏位
        //     ),
        //     //查询模型
        //     "select"=>array(
        //         "command"=>"select",
        //         "table"=>"user",
        //         "clause"=>array("s_no","s_name"),
        //         "column"=>"s_no,s_name,s_mail"
        //     ),
        //     //更新模型
        //     "update"=>array(
        //         "command"=>"update",
        //         "table"=>"user",
        //         "clause"=>array("s_no"),
        //         "update"=>array("s_name","s_mail")
        //     )
        // )
    );

    //@Sonkwl Xiong 2022/02/22 09:10:23
    //@Desc 验证开放模型
    static public function CheckOpen($step,$session_no){
        //没有jwt验证，且当前不是jwt
        if($session_no=='@ALL' && $step["command"]!="jwt"){
            if(!isset(self::$model_open[$step["table"]]) || !isset(self::$model_open[$step["table"]][$step["command"]])){
                self::$json["res"]="NG";
                self::$json["message"]="系统未开放".$step["table"]."的".$step["command"]."功能";
                echo json_encode(self::$json);
                exit;
            }
            //获取模型
            $model_obj=self::$model_open[$step["table"]][$step["command"]];
            if($model_obj["command"]!=$step["command"]){
                self::$json["res"]="NG";
                self::$json["message"]="开放模型".$step["table"]."的".$step["command"]."定义错误";
                echo json_encode(self::$json);
                exit;
            }
            if($model_obj["table"]!=$step["table"]){
                self::$json["res"]="NG";
                self::$json["message"]="开放模型".$step["table"]."的".$step["table"]."定义错误";
                echo json_encode(self::$json);
                exit;
            }
            if(array_keys($model_obj)!=array_keys($step)){
                self::$json["res"]="NG";
                self::$json["message"]=$step["table"]."的".$step["command"]."与系统开放模型定义不一致";
                echo json_encode(self::$json);
                exit;
            }

            //验证clause
            if($step["command"]=="login" || $step["command"]=="delete" || $step["command"]=="insert" || $step["command"]=="update"){
                if($model_obj["clause"]!=array_keys($step["clause"])){
                    self::$json["res"]="NG";
                    self::$json["message"]="开放模型".$step["table"]."使用".$step["command"]."时，clause必须和模型一致";
                    echo json_encode(self::$json);
                    exit;
                }
                foreach($model_obj["clause"] as $v){
                    if(!in_array($v,array_keys($step["clause"]))){
                        self::$json["res"]="NG";
                        self::$json["message"]="开放模型".$step["table"]."的clause中没有".$v;
                        echo json_encode(self::$json);
                        exit;
                    }
                }
                //update
                if($step["command"]=="update" && $model_obj["update"]!=array_keys($step["update"])){
                    self::$json["res"]="NG";
                    self::$json["message"]="开放模型".$step["table"]."使用".$step["command"]."时，update必须和模型一致";
                    echo json_encode(self::$json);
                    exit;
                }
            }
            if($step["command"]=="select" || $step["command"]=="query"){
                foreach($step["clause"] as $k=>$v){
                    if(!in_array($k,$model_obj["clause"])){
                        self::$json["res"]="NG";
                        self::$json["message"]=$step["table"]."的".$step["clause"]."中没有".$k;
                        echo json_encode(self::$json);
                        exit;
                    }
                }
                if(!isset($step["column"]) || !isset($model_obj["column"])){
                    self::$json["res"]="NG";
                    self::$json["message"]="开放的query,select必须定义column";
                    echo json_encode(self::$json);
                    exit;
                }
                //print($step["column"]);
                if($model_obj["column"]!=$step["column"]){
                    self::$json["res"]="NG";
                    self::$json["message"]=$step["table"]."的".$step["column"]."定义与系统开放模型的模型不一致";
                    echo json_encode(self::$json);
                    exit;
                }
            }
        }
    }
}
?>